//
//  SACustomerViewController.m
//  SAP
//
//  Created by 中矿科技有限公司 on 17/9/8.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "SACustomerViewController.h"
#import "SACustomerDetailViewController.h"
#import "SACustomerSerachViewController.h"

#import "SACustomerCell.h"
#import "SACustomerMdoel.h"

@interface SACustomerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@end

static NSString *const cellID =@"SACustomerCell";

@implementation SACustomerViewController
{
    AFHTTPData *_http;
    NSMutableArray *_sourceData;
    NSInteger pageIndex;//页数
    NSInteger pageSize;//一页的显示数
    MBProgressHUD *_waitHud;
    NSString *_keyWord;
    BOOL _isPulling;
    NSString *_startTime;
    NSString *_endTime;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _sourceData =[[NSMutableArray alloc]init];
    _http =[[AFHTTPData alloc]init];
    [self customUI];
    [self pullUp];
    [self pullDown];

}

-(UITableView *)tableView{
    if (_tableView) {
        return _tableView;
    }
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0,0 , SCREEN_WIDTH, SCREEN_HEIGHT-64-44)];
    _tableView.showsHorizontalScrollIndicator =NO;
    _tableView.showsVerticalScrollIndicator =NO;
    _tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
   [_tableView registerNib:[UINib nibWithNibName:@"SACustomerCell" bundle:nil] forCellReuseIdentifier:cellID];
    _tableView.backgroundColor =SA_AllBackColor;
    _tableView.rowHeight =50;
    _tableView.separatorColor =[UIColor colorWithWhite:0.9 alpha:1];
    _tableView.delegate =self;
    _tableView.dataSource =self;
    return _tableView;
}


#pragma mark 刷新界面
-(void)pullDown//下拉页面刷新
{
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isPulling = YES;
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}
-(void)pullUp//上啦页面刷新
{
    __weak __typeof(self) weakSelf = self;
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isPulling = NO;
        [weakSelf loadMoreData];
    }];
}
-(void)loadMoreData{
    pageSize +=7;
    [self prepareData];
}
- (void)loadNewData
{
    
    pageSize =7;
    [self prepareData];
    [self.tableView.mj_footer resetNoMoreData];//恢复数据加载
}
-(void)prepareData{
    
   // if (update)[_waitHud showAnimated:YES];
    NSMutableDictionary *batchDict =[[NSMutableDictionary alloc]init];
    [batchDict setObject:[NSString stringWithFormat:@"%ld",(long)pageSize] forKey:@"maxResultCount"];
    [batchDict setObject:@"0" forKey:@"skipCount"];
    //    [batchDict setObject:@"0" forKey:@"startDate"];
    //    [batchDict setObject:@"0" forKey:@"endDate"];
    
    [_http postTokenDataWithUrl:SAPCompanyListKUrl httpBobyData:batchDict onSuccess:^(NSData *data) {
        NSArray *array =[SACustomerMdoel getCustomerLsitfromData:data];
        if (_isPulling) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            if ([_sourceData count] == [array count]) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }
        [_sourceData removeAllObjects];
        [_sourceData addObjectsFromArray:array];
        [_tableView reloadData];
        [_waitHud showAnimated:NO];
        [_waitHud hideAnimated:YES];
    } onFailure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark UITableView-Deleagte

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _sourceData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SACustomerCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell setFromCustomerListModel:_sourceData[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SACustomerDetailViewController *cDetailedVC =[[SACustomerDetailViewController alloc]init];
    cDetailedVC.customerId =[_sourceData[indexPath.row] Id];
    [self.navigationController pushViewController:cDetailedVC animated:NO];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  118;
}


-(void)onClickedSearchItem{
    UITabBarController * nav  = (UITabBarController*)self.mm_drawerController.centerViewController;
    
    UINavigationController *vc =  nav.viewControllers[0];
    
    SACustomerSerachViewController *SACsearchVC =[[SACustomerSerachViewController alloc]init];
    [vc pushViewController:SACsearchVC animated:NO];
}


-(void)customUI{
    
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"搜索"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickedSearchItem)];
    
    [self.view addSubview:self.tableView];

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
